package com.andorid.Download;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class textActivity extends Activity
{	
	public String PATH=Environment.getExternalStorageDirectory()+ "/download/";
	public String name;
	public ketnoi kn;
	public String duoi;
	public String docChuoi;
	private static final int READ_BLOCK_SIZE=100;
	public EditText edt_text;
	public TextView txt_View;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.text);
		name="text";
		duoi=".txt";
		kn= new ketnoi();
		//File file= new File(PATH,name);
		 kn.DownloadFile("http://10.0.2.2/text.txt",PATH,name,duoi);
		edt_text=(EditText)findViewById(R.id.edt_Text);
		docChuoi=this.Loadfile(name,duoi,PATH);
		edt_text.setText(docChuoi);
		//txt_View.setText(docChuoi);

	}
	public String Loadfile(String name,String duoi,String path)
	{		
		String str ="";
		try {
			// tao duong dan den file
			String nameFile= name+duoi;
			File sdCard= Environment.getExternalStorageDirectory();
			File directory=  new File(path);
			File file= new File(directory, nameFile);
			FileInputStream fIn =new FileInputStream(file);	
			InputStreamReader iSR= new InputStreamReader(fIn);
			char [] inputBuffer= new char[READ_BLOCK_SIZE];
			int charRead;
			//read method of inputstreamreader read number of character and 
			//return -1 if the end of the file is reached
			while((charRead=iSR.read(inputBuffer))>0)
			{//convert char to String 
				
				String readString = String.copyValueOf(inputBuffer,0,charRead);
				str+=readString;
				inputBuffer = new char[READ_BLOCK_SIZE];
			}
			//gan file vao edit text

			Toast.makeText(getBaseContext(), "Load successfull", Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return str;
		
	}
}
